package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @Author likepei
 * @Date 2020/2/5 14:23
 * @Version v1.0
 * @Description 用户接口的实现类
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    //依赖注入
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;

    /**
     * 用户登录
     * @param username Controller传入的用户名
     * @param password Controller传入的密码
     * @return
     */
    @Override
    public boolean login(String username, String password) {
        //日志打印
        log.debug("UserServiceImpl login......u={}...p={}", username, password);
        //学习时使用, 工作中避免使用
        System.out.println("login......u="+username+"...p="+password);

        //用户名与密码的校验
        if("honghong".equals(username) && "123".equals(password)){
            return true;
        } else {
            return false;
        }
    }

    /**
     * 基于用户名 获取User对象
     * @param username
     * @return
     */
    @Override
    public User findByUserName(String username) {
        //通过 用户名获取用户信息
        User user = userDao.findByUsername(username);
        //通过 用户名id  获取所关联的角色集合
        Set<Role> roleSet = roleDao.findByUserId(user.getId());
        user.setRoles(roleSet);
        //通过 每一个角色id  获取所关联的权限集合
        for (Role role : roleSet) {
            Set<Permission> permissionSet = permissionDao.findByRoleId(role.getId());
            role.setPermissions(permissionSet);
        }

        return user;
    }

    @Override
    public List<Map<String,Object>> findMenuByName(String name) {
        List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
       //根据姓名查询菜单
        List<Menu> menuList= menuDao.findMenuByUsername(name);//此时查出所有菜单
        //封装一级菜单
        for (Menu menu : menuList) {
            Integer level = menuDao.findLevelById(menu.getId());
            if (level==1){
                //是一级菜单
               Menu menu1 = menuDao.findById(menu.getId());
               Map<String,Object> menuMap = new HashMap<>();
               menuMap.put("path",menu1.getPath());
               menuMap.put("title",menu.getName());
               menuMap.put("icon",menu.getIcon());
               List<Map<String,Object>> childMapList = new ArrayList<>();
                Menu menuChildren = menuDao.findById(menu.getParentMenuId());
               menuMap.put("children",childMapList);
               mapList.add(menuMap);
            }
        }
        for (Menu menu : menuList) {
            Integer level = menuDao.findLevelById(menu.getId());
            if (level==2){
                //二级菜单
                Menu menu1 = menuDao.findById(menu.getId());
                String s = menu1.getPath().substring(0, 2);
                String path = s.replace("/","");
                for (Map<String, Object> map : mapList) {
                    if (map.get("path").equals(path)){
                        Map<String,Object> childMap = new HashMap<>();
                        childMap.put("path",menu1.getPath());
                        childMap.put("title",menu1.getName());
                        childMap.put("linkUrl",menu1.getLinkUrl());
                        List<Map<String,Object>> childMapList = (List<Map<String, Object>>) map.get("children");
                        childMapList.add(childMap);
                        map.put("children",childMapList);
                    }
                }
            }
        }
        return mapList;
    }
}
